package com.skyline.base.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.base.dao.CommentDao;
import com.skyline.base.dao.NoticeDao;
import com.skyline.base.dao.ShareDao;
import com.skyline.base.model.Comment;
import com.skyline.base.service.CommentService;
import com.skyline.base.type.CommentType;
import com.skyline.base.type.NotificationType;
import com.skyline.base.type.ResourceType;
import com.skyline.common.bean.Page;
import com.skyline.wo.dao.ResourceDao;

/**
 * 评论模块的Service实现
 * 
 * @author Jairus Chan
 * @version 0.1
 * */
@Service("commentService")
public class CommentServiceImpl extends BaseServiceImpl implements CommentService {
	private static final String COMMENT_SUFFIX = "comment";
	@Autowired
	private CommentDao commentDao;

	@Autowired
	private ResourceDao resourceDao;

	@Autowired
	private NoticeDao noticeDao;

	@Autowired
	private ShareDao shareDao;

	private static final Log LOGGER = LogFactory.getLog(CommentServiceImpl.class);

	public void commentResource(Long commenterId, String commenterNickname, String commenterPortrait, String content, Long ref,
			Long resourceId, String resourceName, CommentType commentType, Long ownerId) {
		String resourceComment = commentType.toString().toLowerCase() + COMMENT_SUFFIX;
		commentDao.insertComment(commenterId, commenterNickname, commenterPortrait, content, ref, resourceId, resourceComment, ownerId);

		if (ref != 0) {
			commentDao.updateCommentRef(resourceId, ref, resourceComment);
		}

		try {
			if (commentType.equals(CommentType.SHARE)) {
				LOGGER.debug("评论分享:" + resourceId);
				shareDao.updateCommentNumAdd(resourceId);
			} else {
				try {
					ResourceType resourceType = ResourceType.valueOf(commentType.toString());
					if (resourceType != null) {
						resourceDao.updateCommentNumAdd(resourceType, resourceId);
					}
				} catch (Exception e) {
					LOGGER.warn("", e);
				}
			}
			NotificationType notificationType = NotificationType.valueOf(commentType.toString() + "_COMMENT");
			noticeDao.insertNotication(ownerId, commenterId, commenterNickname, resourceId, resourceName, notificationType);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Comment> lsComments(Page page, Long resourceId, CommentType commentType) {
	//	long t1=System.currentTimeMillis();
		String resourceComment = commentType.toString().toLowerCase() + COMMENT_SUFFIX;
		List<Comment> comments = commentDao.queryCommentByResource(resourceId, page, resourceComment);
		List<Comment> refComments = commentDao.queryRefedCommentByResource(resourceId, resourceComment);
		for (Comment comment : comments) {
			if (comment.getRef() != 0) {
				Comment cursorComment = comment;
				for (Comment refComment : refComments) {
					// TODO: 空指针判断
					if (refComment.getId()!=null&&refComment.getId().equals(cursorComment.getRef())) {
						cursorComment.setRefComment(refComment);
						if (refComment.getRefComment() != null) {
							break;
						}
						cursorComment = refComment;
					}
				}
			}
		}
	//	long t2=System.currentTimeMillis();
	//	System.out.println(t2-t1);
		return comments;
	}

	public void commentAgree(Long resourceId, Long commentId, CommentType commentType) {
		String resourceComment = commentType.toString().toLowerCase() + COMMENT_SUFFIX;
		commentDao.updateCommentAgree(resourceId, commentId, resourceComment);
	}

	public void commentDisagree(Long resourceId, Long commentId, CommentType commentType) {
		String resourceComment = commentType.toString().toLowerCase() + COMMENT_SUFFIX;
		commentDao.updateCommentDisagree(resourceId, commentId, resourceComment);
	}

	public void deleteComment(Long resourceId, Long commentId, CommentType commentType, Long actionerId) {
		if (actionerId != 0) {
			String resourceComment = commentType.toString().toLowerCase() + COMMENT_SUFFIX;
			commentDao.deleteComment(resourceId, commentId, resourceComment, actionerId);
		}
	}
	
	public Comment getCommentById(Long commentId, CommentType commentType) {
		String resourceComment = commentType.toString().toLowerCase() + COMMENT_SUFFIX;
		return commentDao.queryCommentById(commentId, resourceComment);
	}
}
